 package com.printer.bluetooth.android;
 
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.Bitmap.Config;
 import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 import android.graphics.Paint;
 import android.graphics.Paint.Style;
 import android.graphics.RectF;
 import android.graphics.Typeface;
 import com.printer.bluetooth.android.util.Utils;
 import java.util.Locale;
 
 public class PrintGraphics
 {
   private Canvas canvas = null;
 
   public Paint paint = null;
 
   private Bitmap bm = null;
   private int width;
   private float length = 0.0F;
 
   int intPicWidth = 0;
 
   public int getLength()
   {
     return (int)this.length;
   }
 
   public int getWidth()
   {
     return this.width;
   }
 
   public void initCanvas(int w)
   {
     int h = 5 * w;
 
     this.bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
     this.canvas = new Canvas(this.bm);
 
     this.canvas.drawColor(-1);
     this.width = w;
   }
 
   public void initPaint()
   {
     this.paint = new Paint();
 
     this.paint.setAntiAlias(true);
 
     this.paint.setColor(-16777216);
 
     this.paint.setStyle(Paint.Style.STROKE);
   }
 
   public void init(PrinterType printerType)
   {
     if (printerType == PrinterType.T9)
       initCanvas(576);
     else if (printerType == PrinterType.T5)
     {
       initCanvas(240);
     }
     else initCanvas(384);
 
     initPaint();
   }
 
   public void setFontProperty(FontProperty fp)
   {
     if (fp.sFace != null) {
       try {
         this.paint.setTypeface(fp.sFace);
       } catch (Exception e) {
         this.paint.setTypeface(Typeface.DEFAULT);
       }
     }
     else
     {
       this.paint.setTypeface(Typeface.DEFAULT);
     }
 
     if (fp.bBold)
       this.paint.setFakeBoldText(true);
     else {
       this.paint.setFakeBoldText(false);
     }
 
     if (fp.bItalic)
       this.paint.setTextSkewX(-0.5F);
     else {
       this.paint.setTextSkewX(0.0F);
     }
 
     if (fp.bUnderLine)
       this.paint.setUnderlineText(true);
     else {
       this.paint.setUnderlineText(false);
     }
 
     if (fp.bStrikeout)
       this.paint.setStrikeThruText(true);
     else {
       this.paint.setStrikeThruText(false);
     }
 
     this.paint.setTextSize(fp.iSize);
   }
 
   public void setLineWidth(float w)
   {
     this.paint.setStrokeWidth(w);
   }
 
   public void setTextSize(int size) {
     this.paint.setTextSize(size);
   }
 
   public void setItalic(boolean italic) {
     if (italic)
       this.paint.setTextSkewX(-0.5F);
     else
       this.paint.setTextSkewX(0.0F);
   }
 
   public void setStrikeThruText(boolean strike)
   {
     this.paint.setStrikeThruText(strike);
   }
 
   public void setUnderlineText(boolean underline) {
     this.paint.setUnderlineText(underline);
   }
 
   public void setFakeBoldText(boolean fakeBold) {
     this.paint.setFakeBoldText(fakeBold);
   }
 
   public void drawText(float x, float y, String nStr)
   {
     this.canvas.drawText(nStr, x, y, this.paint);
 
     if (this.length < y)
       this.length = y;
   }
 
   public void drawLine(float startX, float startY, float stopX, float stopY)
   {
     this.canvas.drawLine(startX, startY, stopX, stopY, this.paint);
     float max = 0.0F;
     max = startY > stopY ? startY : stopY;
     if (this.length < max)
       this.length = max;
   }
 
   public void drawRectangle(float left, float top, float right, float bottom)
   {
     this.canvas.drawRect(left, top, right, bottom, this.paint);
     float max = 0.0F;
     max = top > bottom ? top : bottom;
     if (this.length < max)
       this.length = max;
   }
 
   public void drawEllips(float left, float top, float right, float bottom)
   {
     RectF re = new RectF(left, top, right, bottom);
     this.canvas.drawOval(re, this.paint);
     float max = 0.0F;
     max = top > bottom ? top : bottom;
     if (this.length < max)
       this.length = max;
   }
 
   public void drawImage(float left, float top, String path)
   {
     try
     {
       Bitmap btm = BitmapFactory.decodeFile(path);
       this.canvas.drawBitmap(btm, left, top, null);
       if (this.length < top + btm.getHeight()) {
         this.length = (top + btm.getHeight());
       }
       this.intPicWidth = btm.getWidth();
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 
   public void drawImage(float left, float top, Context context, int imageId)
   {
     try
     {
       Bitmap btm = BitmapFactory.decodeResource(context.getResources(), imageId);
       this.canvas.drawBitmap(btm, left, top, null);
       if (this.length < top + btm.getHeight()) {
         this.length = (top + btm.getHeight());
       }
       this.intPicWidth = btm.getWidth();
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 
   public void drawImage(float left, float top, Bitmap image)
   {
     try
     {
       this.canvas.drawBitmap(image, left, top, null);
       if (this.length < top + image.getHeight()) {
         this.length = (top + image.getHeight());
       }
       this.intPicWidth = image.getWidth();
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 
   public int saveCanvasImage(String path)
   {
     return Utils.printBitmap2File(getCanvasImage(), path);
   }
 
   public Bitmap getCanvasImage()
   {
     return Bitmap.createBitmap(this.bm, 0, 0, this.width, getLength());
   }
 
   @Deprecated
   public void printPng()
   {
     saveCanvasImage("/sdcard/0.png");
   }
 
   @Deprecated
   public byte[] printDraw() {
     return Utils.bitmap2PrinterBytes(getCanvasImage(), 0);
   }
 
   @Deprecated
   public byte[] printDrawDot() {
     return Utils.bitmap2PrinterBytesDot(getCanvasImage(), 1, 0);
   }
 
   public float getCharacterWidth(int textSize)
   {
     float size = this.paint.getTextSize();
     this.paint.setTextSize(textSize);
     float spacing = this.paint.getFontSpacing();
     this.paint.setTextSize(size);
 
     String lang = Locale.getDefault().getLanguage();
     if ((!lang.equals("ja")) && (!lang.equals("ko")) && (!lang.equals("zh")))
     {
       spacing /= 2.0F;
     }
     return spacing;
   }
 }


